import { useEffect, useState } from 'react';
import { addUserSignInUsingPost } from '@/api/UserApi';
import { message } from 'antd';
import dayjs from 'dayjs';

/**
 * 用户签到Hook
 */
const useAddUserSignIn = () => {
  const [loading, setLoading] = useState<boolean>(true);

  const doFetch = async () => {
    const lastSignInDate = window.localStorage.getItem("lastSignInDate");
    const today = dayjs().format('YYYY-MM-DD'); // 获取今天的日期

    if (lastSignInDate === today) return; // 如果今天已经签到，直接返回

    setLoading(true);
    try {
      await addUserSignInUsingPost({});
      window.localStorage.setItem("lastSignInDate", today); // 存储今天的日期
    } catch (e) {
      message.error('签到失败:' + e);
    }
    setLoading(false);
  };

  useEffect(() => {
    doFetch();
  }, []);

  return { loading };
};

export default useAddUserSignIn;
